package com.qfedu.mtl.dao.impl;


import com.qfedu.mtl.dao.OdersDao;
import com.qfedu.mtl.entity.Orders;
import com.qfedu.mtl.query.OrderQuery;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;

import java.util.ArrayList;
import java.util.List;

/**
 * @version v1.0
 * @Date 2023/9/26 21:17
 * @Author 浪里小白条
 * @Email corday@qq.com
 */
public class OrderDaoImpl implements OdersDao {
    @Override
    public Orders selectAllOrder() {
        return null;
    }


    @Override
    public Integer deleteById(Integer id) {
        String sql="delete from tb_orders where order_id=?";
        return DBUtil.executeDML(sql,DBUtil.getConn(),id);
    }

    @Override
    public int update(Orders orders) {
        String sql="update tb_orders set order_total_price = ?,order_desc = ?,retrieve_type=?,order_status=?" +
                ",send_logistics_name=?,order_processor=?,back_text=?,back_logistics_name=? WHERE order_id=?";
        return DBUtil.executeDML(sql,DBUtil.getConn(),orders.getOrderTotalPrice(),orders.getOrderDesc(),
                orders.getRetrieveType(),orders.getOrderStatus(),orders.getSendLogisticsName(),
                orders.getOrderProcessor(),orders.getBackText(),orders.getBackLogisticsName(),orders.getOrderId() );
    }
//    private Integer retrieve_type; //回收类型
//    private Integer order_status;   //订单状态

    @Override
    public List<Orders> selectPageData(Integer page, Integer size, OrderQuery orderQuery) {
        StringBuilder sqlBuilder=new StringBuilder("select * from tb_orders where 1=1");
        List<Object> values=new ArrayList<>();
        if (orderQuery.getRetrieve_type()!=null){
            sqlBuilder.append(" and retrieve_type=? ");
            values.add(orderQuery.getRetrieve_type());
        }
        if (orderQuery.getOrder_status()!=null){
            sqlBuilder.append(" and order_status=? ");
            values.add(orderQuery.getOrder_status());
        }
        sqlBuilder.append(" limit ?,?");
        values.add( (page-1)*size);
        values.add( size);
        List<Orders> orders = DBUtil.executeDQL(sqlBuilder.toString(), DBUtil.getConn(), new BeanListHandler<>(Orders.class, true), values.toArray());
        return orders;
    }

    @Override
    public Integer selectCount(OrderQuery orderQuery) {
        StringBuilder sqlBuilder = new StringBuilder("select  count(*)  from tb_orders where 1=1 ");
        //存放参数值，顺序和❓ 对应
        List<Object> values = new ArrayList<>();
        //根据名称查询
        if (orderQuery.getRetrieve_type()!=null){
            sqlBuilder.append(" and retrieve_type=? ");
            values.add(orderQuery.getRetrieve_type());
        }
        if (orderQuery.getOrder_status()!=null){
            sqlBuilder.append(" and order_status=? ");
            values.add(orderQuery.getOrder_status());
        }
        Long total = DBUtil.executeDQL(sqlBuilder.toString(), DBUtil.getConn(), new NumberHandler<>(Long.class), values.toArray());
        return total.intValue();
    }
}
